<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

use  App\Model\Text;

class TextController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
         $text = DB::table('shop_text')
        ->join('shop_goods','shop_text.gid','=','shop_goods.id')
        ->select(['shop_text.id','shop_goods.gname','shop_text.attribute'])->paginate(3);
        
        //dd($goods);
       //dd($text);
        
        return view('Admin/TextAdmin/text',['text'=>$text]);

        // /dd($text)
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        //
       $goods = DB::table('shop_goods')->select(['id','gname'])->where('id',$id)->first();
       // /dd($goods->id);
        return view('Admin/TextAdmin/create',['goods'=>$goods]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        
        $gid = $request->gid;
        $attribute = $request->attribute;
        //dd($attribute);
        $shop_text = Text::insertGetId([
                    'gid'=>$gid,
                    'attribute'=>$attribute,
                    
                ]);
         
            if($shop_text==true){

            return redirect('/admin/Text/index')->with('msg','添加成功');
             } else {
                return back()->with('errorTip','添加失败');

            }
                

        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $text = DB::table('shop_text')->select(['id','attribute'])->where('id',$id)->first();
        //dd($text);
        return view('Admin/TextAdmin/edit',['text'=>$text]);
        

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        //dd($request->attribute);
        $attribute = $request->attribute;
        //dd($attribute);
         $bool = DB::table('shop_text')
                ->where('id',$id)
                ->update([
                        'attribute'=>$attribute
                        ]);
                //dd($bool);

                if($bool!==0) {
                        
                         return redirect('/admin/Text/index')->with('msg','修改成功');
                     
                }
                 return back()->with('errorTip','修改失败');
                        
             
        }



    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        //dd($id);
        $bool = DB::table('shop_text')->where('id',$id)->delete();
        if($bool){
                 return redirect('/admin/Text/index')->with('msg','删除成功');
         }

    }
}
